import int as int
from django.shortcuts import render
from django.views import View
from django.contrib.auth.mixins import LoginRequiredMixin

from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

from apps.courses.models import *
from apps.operations.models import *


# 课程列表
class CourseView(View):
    def get(self, request, *args, **kwargs):
        # 所有课程
        all_course = Course.objects.all()
        # 课程总数
        course_nums = Course.objects.count()

        # 热门机构或所有机构
        nav_tab = request.GET.get("nav_tab", "")
        # 取20门热门课程
        all_course_hot = all_course.order_by("-students")[:20]
        # 分类条件
        category = request.GET.get("category", "")
        # 对用户所需的机构类别进行分类
        if category:
            all_course = all_course.filter(category=category)
            course_nums = all_course.count()
        # 排序条件
        sort = request.GET.get("sort", "")
        # 对用户所需的课程类别进行排序
        if sort == "students":
            all_course = all_course.order_by("-students")
        elif sort == "courses":
            all_course = all_course.order_by("-course_num")

        # 对课程进行分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        # 所有课程分页
        p = Paginator(all_course, per_page=8, request=request)
        courses = p.page(page)
        # 热门课程分页
        p_hot = Paginator(all_course_hot, per_page=3, request=request)
        courses_hot = p_hot.page(page)

        return render(request, 'course_list.html', {
            "course_nums": course_nums,
            "courses": courses,
            "courses_hot": courses_hot,
            "category": category,
            "nav_tab": nav_tab,
        })


# 课程详情主页
class CourseHomePageView(View):
    def get(self, request, course_id, *args, **kwargs):
        nav_tab = request.GET.get("nav_tab", "")
        # 获取机构
        course = Course.objects.get(id=course_id)
        # 点击数+1
        course.click_nums += 1
        # 保存更改
        course.save()

        # 判断是否收藏
        has_fav = False
        if request.user.is_authenticated:
            if UserFavorites.objects.filter(user=request.user, fav_id=course.id, fav_type=1):
                has_fav = True

        return render(request, "course-detail-homepage.html", {
            "nav_tab": nav_tab,
            "course": course,
            "has_fav": has_fav,
        })


# 课程章节
class CourseLearnView(LoginRequiredMixin, View):
    login_url = "/login/"

    def get(self, request, course_id, *args, **kwargs):
        # 先判断用户是否登录
        if not request.user.is_authenticated:
            return render("")
        nav_tab = request.GET.get("nav_tab", "")
        # 获取机构
        course = Course.objects.get(id=course_id)
        # 点击数+1
        course.click_nums += 1
        # 保存更改
        course.save()
        # 用户与课程间是否有联系
        user_course = UserCourse.objects.filter(user=request.user, course=course)
        # 用户与课程间没有联系，则将其进行关联
        if not user_course:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()
            # 课程学生数+1
            course.students += 1
            course.save()

        # 课程资源
        course_resources = CourseResources.objects.filter(course=course)
        comments = CourseComments.objects.filter(course=course)

        # 判断是否收藏
        has_fav = False
        if request.user.is_authenticated:
            if UserFavorites.objects.filter(user=request.user, fav_id=course.id, fav_type=1):
                has_fav = True

        return render(request, "course-detail-learn.html", {
            "nav_tab": nav_tab,
            "course": course,
            "has_fav": has_fav,
            "course_resources": course_resources,
            "comments": comments,
        })


# 相关课程
class CourseInterrelatedView(View):
    def get(self, request, course_id, *args, **kwargs):
        nav_tab = request.GET.get("nav_tab", "")
        # 获取机构
        course = Course.objects.get(id=course_id)
        # 点击数+1
        course.click_nums += 1
        # 保存更改
        course.save()
        tags = course.coursetag_set.all()
        tag_list = [tag.tag for tag in tags]
        course_tags = CourseTag.objects.filter(tag__in=tag_list).exclude(course__id=course.id)

        course_inter = set()
        for course_tag in course_tags:
            course_inter.add(course_tag.course)

        # 判断是否收藏
        has_fav = False
        if request.user.is_authenticated:
            if UserFavorites.objects.filter(user=request.user, fav_id=course.id, fav_type=1):
                has_fav = True

        return render(request, "course-detail-interrelated.html", {
            "nav_tab": nav_tab,
            "course": course,
            "has_fav": has_fav,
            "course_inter": course_inter,
        })


# 视频播放
class CourseVideoView(View):
    login_url = "/login/"

    def get(self, request, course_id, video_id, *args, **kwargs):
        # 先判断用户是否登录
        if not request.user.is_authenticated:
            return render("")
        nav_tab = request.GET.get("nav_tab", "")
        # 获取机构
        course = Course.objects.get(id=course_id)
        # 点击数+1
        course.click_nums += 1
        # 保存更改
        course.save()

        video = Video.objects.get(id=video_id)

        return render(request, "course-video.html", {
            "nav_tab": nav_tab,
            "course": course,
            "video": video,
        })
